<template>
  <div class="teaching">
    <el-select
      class="te-select"
      style="width: 97%"
      v-model="value"
      popper-class="te-list"
      @change="change"
    >
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      >
      </el-option>
    </el-select>
    <div class="te-Title">
      <div class="titlecolor"></div>
      <div class="te">
        <h1 class="tetitle">{{ title }}</h1>
      </div>
    </div>
    <div class="te-content">
      <template v-if="show == 1">
        <div>稍等</div>
      </template>
      <template v-if="show == 2">
        <li
          v-for="{ title, id, time } in innovates"
          class="list-item-teaching"
          @click="article1(id)"
          :key="id"
        >
          <p class="list-item-title">{{ title }}</p>
          <i class="arrow1 el-icon-arrow-right"></i>
          <span class="list-item-time">{{ time }}</span>
        </li>
      </template>
      <template v-if="show == 3">
        <li
          v-for="{ title, id, time } in masters"
          class="list-item-teaching"
          @click="article2(id)"
          :key="id"
        >
          <p class="list-item-title">{{ title }}</p>
          <i class="arrow1 el-icon-arrow-right"></i>
          <span class="list-item-time">{{ time }}</span>
        </li>
      </template>
      <template v-if="show == 4">
        <div class="article">
          <div>
            <template>
              <h2 class="article-news-title">{{ innovates[id - 1].title }}</h2>
              <div class="article-center">
                时间：
                <!-- {{ news.author }} -->
                &ensp;
                {{ innovates[id - 1].time }}
              </div>
              <hr />
              <p
                class="article-news-content"
                v-html="innovates[id - 1].content"
              ></p>
            </template>
          </div>
          <div class="previous-next">
            <p @click="previous1(id)">上一条：{{ previous }}</p>
            <p @click="next1(id)">下一条：{{ next }}</p>
          </div>
        </div>
      </template>
      <template v-if="show == 5">
        <div class="article">
          <div>
            <template>
              <h2 class="article-news-title">{{ masters[id - 1].title }}</h2>
              <div class="article-center">
                时间：
                <!-- {{ news.author }} -->
                &ensp;
                {{ masters[id - 1].time }}
              </div>
              <hr />
              <p
                class="article-news-content"
                v-html="masters[id - 1].content"
              ></p>
            </template>
          </div>
          <div class="previous-next">
            <p @click="previous1(id)">上一条：{{ previous }}</p>
            <p @click="next1(id)">下一条：{{ next }}</p>
          </div>
        </div>
      </template>
    </div>
  </div>
</template>

<script>
import { innovate, master } from "@/api";
export default {
  name: "Teaching",
  data() {
    return {
      options: [
        { value: "1", label: "教学科研网" },
        { value: "2", label: "创新平台" },
        { value: "3", label: "名师工作室" },
      ],
      value: this.$route.query.key,
      title: this.$route.query.key,
      show: this.$route.query.show,
      id: this.$route.query.id,
      innovates: [],
      masters: [],
    };
  },
  created() {
    innovate().then((res) => {
      const innovate = res.data;
      this.innovates = innovate;
    });
    master().then((res) => {
      const master = res.data;
      this.masters = master;
    });
  },
  mounted() {
    this.$emit("teaching1", false);
  },
  beforeDestroy() {
    this.$emit("teaching", true);
  },
  watch: {
    $route(to, from) {
      this.show = to.query.show;
      this.title = to.query.key;
      this.value = to.query.key;
      this.id = to.query.id;
    },
  },
  computed: {
    // 前一条数据
    previous: {
      get() {
        if (this.title == "创新平台") {
          if (this.innovates[this.id - 1].id == 1) {
            return "已经到最前面~";
          } else {
            return this.innovates[this.id - 2].title;
          }
        } else if (this.title == "名师工作室") {
          if (this.masters[this.id - 1].id == 1) {
            return "已经到最前面~";
          } else {
            return this.masters[this.id - 2].title;
          }
        } else {
          console.error("错误,没有该条目");
        }
      },
    },
    // 后一条数据
    next: {
      get() {
        if (this.title == "创新平台") {
          if (this.innovates[this.id] == undefined) {
            return "后面没有新闻~";
          } else {
            return this.innovates[this.id].title;
          }
        } else if (this.title == "名师工作室") {
          if (this.masters[this.id] == undefined) {
            return "后面没有新闻~";
          } else {
            return this.masters[this.id].title;
          }
        } else {
          console.error("错误,没有该条目");
        }
      },
    },
  },
  methods: {
    // 选择框值发生改变
    change(value) {
      let obj = {};
      obj = this.options.find((item) => {
        return item.value === value;
      });
      this.value = obj.label;
      this.title = this.value;
      this.$router.replace({
        query: { key: this.title, show: obj.value },
      });
      if (obj.value == 1) {
        window.location.href = "https://www.sdwcvc.edu.cn/jwkyc/";
      }
    },
    // 跳转创新平台文章
    article1(id) {
      // console.log(this.innovates[id - 1]);
      this.$router.push({
        query: { key: "创新平台", show: 4, id: id },
      });
    },
    // 跳转名师工作室文章
    article2(id) {
      this.$router.push({
        query: { key: "名师工作室", show: 5, id: id },
      });
    },
    // 向后跳转一页
    next1(id) {
      ++id;
      if (this.title == "创新平台" && this.innovates[id - 1] != undefined) {
        this.$router.push({
          query: { key: "创新平台", show: 4, id: id },
        });
      } else if (
        this.title == "名师工作室" &&
        this.masters[id - 1] != undefined
      ) {
        this.$router.push({
          query: { key: "名师工作室", show: 5, id: id },
        });
      }
    },
    // 向前跳转一页
    previous1(id) {
      --id;
      if (this.title == "创新平台" && this.innovates[id].id != 1) {
        this.$router.push({
          query: { key: "创新平台", show: 4, id: id },
        });
      } else if (this.title == "名师工作室" && this.masters[id].id != 1) {
        this.$router.push({
          query: { key: "名师工作室", show: 5, id: id },
        });
      }
    },
  },
};
</script>

<style>
/* 教学科研 */
.teaching {
  background: white;
  padding: 0.2rem;
}
/* 标题栏 */
.te-Title {
  width: 96%;
  border-bottom: 1px solid #084fb0;
  margin: 0.2rem;
}
/* 标题容器 */
.te {
  display: inline-block;
}
/* 标题 */
.tetitle {
  font-size: 0.8rem;
  padding: 0.2rem;
  color: #084fb0;
  font-weight: normal;
}
/* 内容容器 */
.te-content {
  margin-top: 0.72rem;
}
/* 列表 */
.list-item-teaching {
  list-style: none;
  line-height: 1rem;
  font-size: 0.56rem;
  padding-left: 0.4rem;
  padding-bottom: 0.36rem;
  border-bottom: 0.04rem dashed #eeeeee;
  margin-bottom: 0.72rem;
}
/* 列表标题 */
.list-item-teaching .list-item-title {
  width: 88%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
/* 列表时间 */
.list-item-teaching .list-item-time {
  font-size: 0.4rem;
  color: #c3c3c3;
}
/* 选择框 */
.te-select .el-input__inner {
  text-align: center;
  background: #084fb0;
  color: white;
  height: 1.5rem;
  font-size: 0.72rem;
}
/* 选择栏容器 */
.te-list {
  text-align: center;
  background: rgba(0, 0, 0, 0.75);
}
/* 单个选择栏 */
.te-list .el-select-dropdown__item {
  color: white;
  border-bottom: 1px solid #868686;
  font-size: 0.6rem !important;
  height: auto !important;
  line-height: 1.5rem !important;
}
/* 最后一个选项框去掉底部边框线 */
.te-list .el-select-dropdown__item:last-child {
  border-bottom: 0;
}
/* 选择框选择栏选中后 */
.te-list .el-select-dropdown__item.hover {
  background-color: rgba(0, 0, 0, 0.75);
}
/* 文章里面的视频大小 */
.article .article-news-content div video {
  width: 100%;
}
/* 上一条和下一条 */
.article .previous-next {
  font-size: 0.5rem;
}
.article .previous-next p:last-child {
  padding-bottom: 0.72rem;
}
</style>
